package com.mx.tool.form.window.jy;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ReUtil;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @Author: 徐建鹏.
 * @create: 2021-11-10 16:58
 * @Description:
 */
public class ZzWindow {
    private JPanel jPanel;
    private JTextArea contentArea;
    private JTextArea showArea;
    private JTabbedPane tabbedPane;
    private JComboBox<String> comboBox;
    private JTextField contentText;
    private JTextField resultTest;
    private JButton searchButton;
    private JButton replaceButton;
    private JList<String> list1;
    private JCheckBox qjCheckBox;
    private static final Map<String,String> zzMap = new HashMap<>();

    static {
        zzMap.put("中文字符","[\\u4e00-\\u9fa5]");
        zzMap.put("双字节字符包括汉字","[^\\x00-\\xff]");
        zzMap.put("邮箱地址","[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?");
        zzMap.put("网址url","[a-zA-z]+://[^\\s]*");
        zzMap.put("手机","[1][3,4,5,7,8][0-9]{9}");
        zzMap.put("IP地址","\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
        zzMap.put("国内电话","\\d{3}-\\d{8}|\\d{4}-\\d{7,8}");
        zzMap.put("QQ号","[1-9][0-9]{4,}");
        zzMap.put("邮政编码","[1-9]\\d{5}(?!\\d)");
        zzMap.put("正整数","[1-9]\\d*");
        zzMap.put("负整数","-[1-9]\\d*");
        zzMap.put("整数","-?[1-9]\\d*");
        zzMap.put("非负整数","[1-9]\\d*|0");
        zzMap.put("非正整数","-[1-9]\\d*|0");
        zzMap.put("正浮点数","[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*");
        zzMap.put("负浮点数","-[1-9]\\d*\\.\\d*|-0\\.\\d*[1-9]\\d*");
    }


    public ZzWindow() {
        contentText.setText(zzMap.get("中文字符"));
        comboBox.addActionListener(e -> {
            String typeStr = Convert.toStr(comboBox.getSelectedItem());
            String zzbds = zzMap.get(typeStr);
            if(StringUtils.isNotBlank(zzbds)){
                contentText.setText(zzbds);
            }
        });
        searchButton.addActionListener(e -> {
            showArea.setText("");
            String contentStr = contentArea.getText();
            String zzbds = contentText.getText();
            if(StringUtils.isNotBlank(contentStr) && StringUtils.isNotBlank(zzbds)){
                StringBuilder stringBuilder;
                if(qjCheckBox.isSelected()){
                    List<String> resultList = ReUtil.findAllGroup0(zzbds,contentStr);
                    stringBuilder = new StringBuilder("共找到 "+resultList.size()+" 处匹配：");
                    for(String str : resultList){
                        stringBuilder.append("\n").append(str);
                    }
                }else{
                    String result = ReUtil.getGroup0(zzbds,contentStr);
                    int index = contentStr.indexOf(result);
                    stringBuilder = new StringBuilder("匹配位置："+index);
                    stringBuilder.append("\n").append(result);
                }
                showArea.setText(stringBuilder.toString());
            }
        });

        replaceButton.addActionListener(e -> {
            showArea.setText("");
            String contentStr = contentArea.getText();
            String zzbds = contentText.getText();
            if(StringUtils.isNotBlank(contentStr) && StringUtils.isNotBlank(zzbds)){
                String resultStr;
                if(qjCheckBox.isSelected()){
                     resultStr = ReUtil.replaceAll(contentStr,zzbds,resultTest.getText());
                }else{
                    resultStr = ReUtil.replaceFirst(Pattern.compile(zzbds),contentStr,resultTest.getText());
                }
                showArea.setText(resultStr);
            }
        });
    }

    public JPanel getContent() {
        return jPanel;
    }
}
